package com.enlorenz.common.security.des;
import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.enlorenz.common.security.Coder4Base;
import com.enlorenz.common.util.CodeUtils;


public class BaseDesUtils {
     /** 
     * 生成密钥 
     * @return 
     * @throws Exception 
     */  
    public static byte [] initKey(String desType){  
        return initKey(null,desType);  
    }  
  
    /** 
     * 生成密钥 
     *  
     * @param seed 
     * @return 
     * @throws Exception 
     */  
    public static byte [] initKey(String seed,String desType){  
    	try { 
	        SecureRandom secureRandom = null;  
	        if (seed != null) {  
	            secureRandom = new SecureRandom(Coder4Base.decryptBASE64(seed));  
	        } else {  
	            secureRandom = new SecureRandom();  
	        }  
	        KeyGenerator kg = KeyGenerator.getInstance(desType);  
	        kg.init(secureRandom);  
	        SecretKey secretKey = kg.generateKey();  
	        byte [] result= secretKey.getEncoded();
	        return result;
    	}catch (Exception e) { 
			throw new RuntimeException(e);
		} 
    } 
    
    public static String initKeyByBase64(String desType){
    	return initKeyByBase64(null,desType);
    }
    
    public static String initKeyByBase64(String seed,String desType){
    	byte [] buff=initKey(seed, desType);
    	return Coder4Base.encryptBase64(buff);
    }
    
    public static String initKeyByHex(String desType){
    	return initKeyByHex(null,desType);
    }
    
    public static String initKeyByHex(String seed,String desType){
    	byte [] buff=initKey(seed, desType);
    	return CodeUtils.bytes2HexString(buff);
    }
    

    /** 
     * 加密 
     * @param data 数据
     * @param key 密钥
     * @return 加密后数据
     * @throws Exception 
     */  
    public static byte[] encrypt(byte[] data, byte [] key,String desType,String desName) {  
    	return doCipher(data,key,Cipher.ENCRYPT_MODE,desType,desName); 
    }  
    
    /** 
     * 解密 
     * @param data 数据
     * @param key 密钥
     * @return 解密后数据
     * @throws Exception 
     */  
    public static byte[] decrypt(byte[] data,byte [] key,String desType,String desName){ 
	    return doCipher(data,key,Cipher.DECRYPT_MODE,desType,desName); 
    }
    

    

	public static byte[] doCipher(byte[] data,byte [] key,int cipherType,String desType,String desName) {
		Key k = toKey(key,desName);
		try { 
	        Cipher cipher = Cipher.getInstance(desType);  
	        cipher.init(cipherType, k);  
	        return cipher.doFinal(data);  
    	}catch (Exception e) { 
			throw new RuntimeException(e);
		}
	}  
	

    /** 
     * 转换密钥<br> 
     *  
     * @param key 
     * @return 
     * @throws Exception 
     */  
    public static Key toKey(byte[] key,String desType){  
       /* DESKeySpec dks = new DESKeySpec(key);  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(desType.name());  
        SecretKey secretKey = keyFactory.generateSecret(dks);*/
        // 当使用其他对称加密算法时，如AES、Blowfish等算法时，用下述代码替换上述三行代码  
        SecretKey secretKey = new SecretKeySpec(key, desType);  
        return secretKey;  
    }  
}  